package com.leyou.order.web;

import com.leyou.common.dto.OrderDTO;
import com.leyou.order.pojo.Order;
import com.leyou.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("order")
public class OrderController {

    @Autowired
    private OrderService orderService;


    /**
     * 新增订单
     *
     * @param orderDTO
     * @return
     */
    @PostMapping
    public ResponseEntity<Long> createOrder(@RequestBody OrderDTO orderDTO) {
        Long orderId = orderService.createOrder(orderDTO);
        return ResponseEntity.status(HttpStatus.CREATED).body(orderId);
    }

    /**
     * 根据订单id查询订单
     *
     * @param orderId
     * @return
     */
    @GetMapping("{orderId}")
    public ResponseEntity<Order> queryOrderById(@PathVariable("orderId") Long orderId) {
        return ResponseEntity.ok(orderService.queryOrderById(orderId));
    }

    /**
     * 根据订单id生成支付url
     *
     * @param orderId
     * @return
     */
    @GetMapping("url/{orderId}")
    public ResponseEntity<String> generatePayUrl(@PathVariable("orderId") Long orderId) {
        return ResponseEntity.ok(orderService.generatePayUrl(orderId));
    }




    @GetMapping("state/{orderId}")
    public ResponseEntity<Integer> queryPayStateByOrderId(@PathVariable("orderId") Long orderId) {
        return ResponseEntity.ok(orderService.queryPayStateByOrderId(orderId));
    }

}
